package com.atguigu.aiproject.controller.admin;

import com.atguigu.aiproject.entity.Result;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class EmailController {
    @Value("${spring.mail.username}")
    private String from;//发件人
    @Resource
    private JavaMailSender mailSender;
    /**
     * 发送邮件
     *
     * @param to      收件人
     * @param subject 邮件主题
     * @param content 邮件内容
     * @return
     */
    @PostMapping("/email")
    public Result<?> sendEmail(String to, String subject, String content) {
        SimpleMailMessage mail = new SimpleMailMessage();//创建邮件对象
        mail.setFrom(from);//发件人
        mail.setTo(to);//收件人
        mail.setSubject(subject);//邮件主题
        mail.setText(content);//邮件内容
        mailSender.send(mail);//发送邮件
        return Result.success();
    }
}
